package com.coalmine.activiti.webadmin.upms.controller;

import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.coalmine.activiti.common.flow.model.FlowEntry;
import com.coalmine.activiti.common.flow.service.FlowEntryService;
import com.coalmine.activiti.webadmin.upms.model.ActDeBusiness;
import com.coalmine.activiti.webadmin.upms.model.Table;
import com.coalmine.activiti.webadmin.upms.service.IActDeBusinessService;
import com.coalmine.activiti.webadmin.upms.service.impl.TableService;
import com.coalmine.common.core.utils.poi.ExcelUtil;
import com.coalmine.common.core.web.controller.BaseController;
import com.coalmine.common.core.web.domain.AjaxResult;
import com.coalmine.common.core.web.page.TableDataInfo;

/**
 * 业务类型Controller
 *
 * @author coalmine
 * @date 2021-08-21
 */
@RestController
@RequestMapping("/business")
public class ActDeBusinessController extends BaseController
{
    @Autowired
    private IActDeBusinessService actDeBusinessService;

	@Autowired 
	private FlowEntryService flowEntryService;
	
	@Autowired
    TableService tableService;

    

    /**
     * 查询业务类型列表
     */
    @GetMapping("/list")
    public TableDataInfo list(ActDeBusiness actDeBusiness)
    {
        startPage();
        List<ActDeBusiness> list = actDeBusinessService.selectActDeBusinessList(actDeBusiness);
        return getDataTable(list);
    }
    /**
     * 查询流程定义列表
     */
    @GetMapping("/getProcdefList")
    public TableDataInfo getProcdefList(FlowEntry flowEntry)
    {
        startPage();
        List<FlowEntry> list = flowEntryService.getListByFilter(flowEntry);
        return getDataTable(list);
    }
    
    /**
     * 查询业务表单列表
     */
//    @GetMapping("/getTableList")
//    public TableDataInfo getTableList(FormConfig formConfig)
//    {
//        startPage();
//        List<FormConfig> list = flowEntryService.getTableList(formConfig);
//        return getDataTable(list);
//    }
    
    /**
     * 查询业务表列表
     * @param tableName
     * @return
     */
    @RequestMapping(value = "getTableList")
    public TableDataInfo getTableList(@RequestParam(name = "tableName", required = false) String tableName) {
        startPage();
        List<Table> list = tableService.getTable(tableName);
        return getDataTable(list);
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/queryById")
    public AjaxResult queryById(@RequestParam(name="id",required=true) String id) {
        ActDeBusiness actDeBusiness = actDeBusinessService.selectActDeBusinessById(id);
        if(actDeBusiness==null) {
            return AjaxResult.error("未找到对应数据");
        }
        List<FlowEntry> modelList = flowEntryService.getAllList();
        FlowEntry flowEntry = modelList.stream().filter((n) -> {
            return n.getProcessDefinitionKey().equals(actDeBusiness.getModelKey());
        }).findFirst().get();
        actDeBusiness.setModelName(flowEntry.getProcessDefinitionName());
        return AjaxResult.success(actDeBusiness);
    }

    /**
     * 导入业务类型数据
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<ActDeBusiness> util = new ExcelUtil<ActDeBusiness>(ActDeBusiness.class);
        List<ActDeBusiness> businessList = util.importExcel(file.getInputStream());
        String message = actDeBusinessService.importActDeBusiness(businessList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 导入业务类型模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<ActDeBusiness> util = new ExcelUtil<ActDeBusiness>(ActDeBusiness.class);
        util.importTemplateExcel(response, "业务类型数据");
    }

    /**
     * 导出业务类型列表
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, ActDeBusiness actDeBusiness) throws IOException
    {
        List<ActDeBusiness> list = actDeBusinessService.selectActDeBusinessList(actDeBusiness);
        ExcelUtil<ActDeBusiness> util = new ExcelUtil<ActDeBusiness>(ActDeBusiness.class);
        util.exportExcel(response, list, "业务类型数据");
    }

    /**
     * 获取业务类型详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return AjaxResult.success(actDeBusinessService.selectActDeBusinessById(id));
    }

    /**
     * 新增业务类型
     */
    @PostMapping
    public AjaxResult add(@RequestBody ActDeBusiness actDeBusiness)
    {
//        Integer count = actDeBusinessService.findByDefIdAndTableName(actDeBusiness.getBusinessTableName());
//        if(count!=null&&count>0){
//            return AjaxResult.error("表名已经存在！");
//        }
        return toAjax(actDeBusinessService.insertActDeBusiness(actDeBusiness));
    }

    /**
     * 修改业务类型
     */
    @PostMapping("/update")
    public AjaxResult edit(@RequestBody ActDeBusiness actDeBusiness)
    {
    	 /*
        ActDeBusiness dbEntity = actDeBusinessService.selectActDeBusinessById(actDeBusiness.getId());
        if(StringUtils.isNotBlank(dbEntity.getBusinessTableName())){
            //如果当前表名和数据库表名不一致，则校验，该表名是否已经存在
            if(!dbEntity.getBusinessTableName().equals(actDeBusiness.getBusinessTableName())){
                Integer count = actDeBusinessService.findByDefIdAndTableName(actDeBusiness.getBusinessTableName());
                if(count!=null&&count>0){
                    return AjaxResult.error("表名已经存在！");
                }
            }
        }*/
        return toAjax(actDeBusinessService.updateActDeBusiness(actDeBusiness));
    }

    /**
     * 删除业务类型
     */
	@PostMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(actDeBusinessService.deleteActDeBusinessByIds(ids));
    }
    
    /**
     * 查询业务类型的部门列表ID
     */
    @GetMapping(value = "/actDeptTreeselect/{actId}")
    public AjaxResult actDeptTreeselect(@PathVariable("actId") String actId)
    {
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", actDeBusinessService.selectDeptListByActId(actId));
        return ajax;
    }
}
